package com.css.fxfzaqbz.util;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;

import java.util.List;

/**
 * @author y1fan
 * @create 2022-12-21 16:53
 * Word插入表格数据的工具
 */

public class WordUtil {

    /**
     * 为表格插入数据，行数不够添加新行
     *
     * @param table     需要插入数据的表格
     * @param tableList 插入数据集合
     */
    public static void insertTable(XWPFTable table, List<List<String>> tableList) {
        //创建行,根据需要插入的数据添加新行，不处理表头
        for (int i = 1; i <= tableList.size(); i++) {
            table.createRow();
        }
        //遍历表格插入数据
        List<XWPFTableRow> rows = table.getRows();
        for (int i = 1; i < rows.size(); i++) {
            XWPFTableRow newRow = table.getRow(i);
            List<XWPFTableCell> cells = newRow.getTableCells();
            for (int j = 0; j < cells.size(); j++) {
                XWPFTableCell cell = cells.get(j);
                cell.setText(tableList.get(i - 1).get(j));
                //表格样式一致-->没有此段表格会默认左对齐
                //有此段会使表格格式一致
                CTTc cttc = cell.getCTTc();
                CTTcPr ctPr = cttc.addNewTcPr();
                ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
                cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
            }
        }
    }

    public static void insertTable1(XWPFTable table, List<List<String>> tableList) {
        for (int i = 0; i < tableList.size(); i++) {
            XWPFTableRow row = table.createRow();
            row.setCantSplitRow(true);
            int twipsPerInch = 1440;
            row.setHeight(twipsPerInch * 1 / 10);
            for (int j = 0; j < tableList.get(i).size(); j++) {
                try {
                    XWPFTableCell cell = row.getCell(j);
                    List<XWPFParagraph> paragraphs = cell.getParagraphs();
                    for (XWPFParagraph paragraph : paragraphs) {
                        XWPFRun run = paragraph.createRun();
                        run.setText(tableList.get(i).get(j));
                        run.setFontSize(11);
                        run.setFontFamily("仿宋_GB2312");
                        paragraph.setAlignment(ParagraphAlignment.CENTER);
                    }
                    cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
                } catch (Exception e) {
                    XWPFTableCell cell = row.addNewTableCell();
                    List<XWPFParagraph> paragraphs = cell.getParagraphs();
                    for (XWPFParagraph paragraph : paragraphs) {
                        XWPFRun run = paragraph.createRun();
                        run.setText(tableList.get(i).get(j));
                        run.setFontSize(11);
                        run.setFontFamily("仿宋_GB2312");
                        paragraph.setAlignment(ParagraphAlignment.CENTER);
                    }
                    cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
                }
                CTTc cttc = row.getCell(j).getCTTc();
                CTTcPr ctPr = cttc.addNewTcPr();
                ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
                cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
            }
        }
    }

    public static void insertTable2(XWPFTable table, List<List<String>> qyList) {
        //创建行,根据需要插入的数据添加新行，不处理表头
        for (int i = 1; i <= qyList.size(); i++) {
            XWPFTableRow row = table.createRow();
            row.setHeight(20 * 10);
        }
        for (int i = 0; i < qyList.size(); i++) {
            List<String> strings = qyList.get(i);
            XWPFTableRow row = table.getRow(i + 1);
            //设置表格内容不会跨页换行
            row.setCantSplitRow(true);
            for (int j = 0; j < strings.size(); j++) {
                XWPFTableCell cell = row.getCell(j);
                List<XWPFParagraph> paragraphs = cell.getParagraphs();
                for (XWPFParagraph xwpfParagraph : paragraphs) {
                    xwpfParagraph.setAlignment(ParagraphAlignment.CENTER);
                    XWPFRun run = xwpfParagraph.createRun();
                    run.setText(qyList.get(i).get(j));
                    run.setFontSize(11);
                    run.setFontFamily("仿宋_GB2312");

                }
                cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
                CTTc cttc = cell.getCTTc();
                CTTcPr ctPr = cttc.addNewTcPr();
                ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
                cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
            }
        }
    }
}
